$(function(){
	// fade out all flash messages after x seconds
	$("#flashMessage").fadeTo(5000, 1).fadeOut(2000);
	
	$(".popup .button").click(function(){
		$(this.parentNode).fadeOut('slow');
		return false;
	});
	$("#searchClose").click(function(){
		$('#NewzQuery').attr('value', '');
	})
	initSearch();
});



function popup(options) {
	$(options.selector).css('top', (options.position-20) + 'px').fadeIn(500).children(".msg").empty().append(options.msg);
}

function initInfoLink(){
	$("#results li").click(function(){
		$(this).toggleClass('active').children(".description").slideToggle();
	})
}

function initSearch(){
	$('#newzForm').submit(function(){
		var query = $('#NewzQuery').val();
		$.ajax({
			type: "GET",
			url: $(this).attr('action'),
			data: {'data[Newz][query]': query},
			beforeSend: function(){
				$("#loading_image").show();
			},
			success: handleResults,
			error: function(e){
				$("#results").text('There was an error connecting to the HellaNZB server');
			},
			dataType: 'html'
		});
		return false;
	});
}

function handleResults(results){
	$('#loading_image').fadeOut();
	$('#results').show().html(results);
	hellaLink();
	initInfoLink();
}